/*
 * 原版接口
 */
import * as server from "@minecraft/server";
/*
 * 蛋白石组件
 */
import * as native_tools from "../system/native_tools";
import * as math_tools from "../system/math_tools";
/**
 * * 进行检测的默认标签
 */
const defaultTag = 'data_tag:magic_tool.series';
/**
 * * 方块破坏组件参数解构
 */
interface DESTROY_COMPONENT {
    /**
     ** 方块状态
     */
    state: server.BlockPermutation,
    /**
     ** 方块维度
     */
    dimension: server.Dimension,
    /**
     ** 玩家对象
     */
    player: server.Player | undefined,
    /**
     ** 方块对象
     */
    block: server.Block,
    /**
     ** 物品对象
     */
    item: server.ItemStack | undefined;
};
/**
 * * 方块破坏组件触发器
 * 
 * @param source - 方块组件参数
 */
function DestroyComponentTrigger(source: server.BlockComponentPlayerDestroyEvent): DESTROY_COMPONENT {
    /**
     ** 方块状态
     */
    const state = source.destroyedBlockPermutation;
    /**
     ** 方块维度
     */
    const dimension = source.dimension;
    /**
     ** 玩家对象
     */
    const player = source.player;
    /**
     ** 方块对象
     */
    const block = source.block;
    /**
     ** 物品对象
     */
    const item = player?.getComponent('minecraft:inventory')?.container?.getItem(player.selectedSlotIndex);
    // 返回 方块破坏组件 的 参数解构
    return { state, dimension, player, block, item };
};
/**
 ** 魔晶储罐 - 方块破坏
 */
export function CrystalTankTrigger(): server.BlockCustomComponent {
    return {
        onPlayerDestroy(source: server.BlockComponentPlayerDestroyEvent) {
            /**
             * * 方块破坏组件参数解构
             */
            const analysis = DestroyComponentTrigger(source);
            // 检测是否使用了正确道具
            if (!analysis.item?.hasTag(defaultTag)) return;
            /**
             ** 物品生成锚点
             */
            const anchor = math_tools.Vector.toString(analysis.block.above()?.bottomCenter() as server.Vector3, { delimiter: ' ' });
            // 随机生成魔晶石
            analysis.dimension.runCommand(`loot spawn ${anchor} loot "energy_crystal/random"`);
            // 播放音效 与 粒子效果
            analysis.block.dimension?.playSound('cauldron.explode', analysis.block.location);
            // 魔晶储罐被破坏时的粒子效果
            switch (math_tools.Random({ max: 4, min: 0 }, true)) {
                case 0:
                    native_tools.TrySpawnParticle(analysis.dimension, 'constant:fireworks_fireball_rune_red', analysis.block.above()?.bottomCenter() as server.Vector3);
                    native_tools.TrySpawnParticle(analysis.dimension, 'constant:fireworks_paper_rune_red', analysis.block.above()?.bottomCenter() as server.Vector3);
                    break;

                case 1:
                    native_tools.TrySpawnParticle(analysis.dimension, 'constant:fireworks_fireball_rune_blue', analysis.block.above()?.bottomCenter() as server.Vector3);
                    native_tools.TrySpawnParticle(analysis.dimension, 'constant:fireworks_paper_rune_blue', analysis.block.above()?.bottomCenter() as server.Vector3);
                    break;

                case 2:
                    native_tools.TrySpawnParticle(analysis.dimension, 'constant:fireworks_fireball_rune_green', analysis.block.above()?.bottomCenter() as server.Vector3);
                    native_tools.TrySpawnParticle(analysis.dimension, 'constant:fireworks_paper_rune_green', analysis.block.above()?.bottomCenter() as server.Vector3);
                    break;

                case 3:
                    native_tools.TrySpawnParticle(analysis.dimension, 'constant:fireworks_fireball_rune_orange', analysis.block.above()?.bottomCenter() as server.Vector3);
                    native_tools.TrySpawnParticle(analysis.dimension, 'constant:fireworks_paper_rune_orange', analysis.block.above()?.bottomCenter() as server.Vector3);
                    break;

                case 4:
                    native_tools.TrySpawnParticle(analysis.dimension, 'constant:fireworks_fireball_rune_purple', analysis.block.above()?.bottomCenter() as server.Vector3);
                    native_tools.TrySpawnParticle(analysis.dimension, 'constant:fireworks_paper_rune_purple', analysis.block.above()?.bottomCenter() as server.Vector3);
                    break;

                default: break;
            }
        }
    }
}